//
//  DDSendView.m
//  ddhw_ios
//
//  Created by sv on 17/9/18.
//  Copyright © 2017年 DDTM. All rights reserved.
//

#import "DDSendView.h"
//#import "DDTextView.h"


@interface DDSendView()<UITextViewDelegate>

//@property (nonatomic, strong) DDTextView *textView;
@property (nonatomic, strong) UIButton *sendBtn;
@property (nonatomic, strong) UIView *lineView;

@end
@implementation DDSendView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.lineView];
        [self addSubview:self.sendBtn];
        [self addSubview:self.textView];
    }
    return self;
}
- (void)sendBtnClick
{
    [self.textView endEditing:YES];
   
    NSString *str = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    self.textView.text = @"";
    
    
    if(NULLString(str))
    {
        [self.window makeToast:@"您还没有输入内容!"];
//        [CustomDialog showToast:DDGKeyWindow msg:@"您还没有输入内容!"];
        return;
    }
    
    if([self.delegate respondsToSelector:@selector(sendViewDidClickSend:contentStr:)])
    {
        [self.delegate sendViewDidClickSend:self contentStr:str];
        self.sendBtn.enabled = NO;
    }
}


- (void)textViewDidChange:(UITextView *)textView
{
    if(textView.hasText)
    {
        NSLog(@"我有值");
        self.sendBtn.enabled = YES;
        
    }else{
        NSLog(@"我空了");
        self.sendBtn.enabled = NO;
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.lineView.x = 0;
    self.lineView.width = self.width;
    
    self.sendBtn.y = ScaleH(5);
    self.sendBtn.x = self.width - self.sendBtn.width;
    

    self.textView.x = ScaleW(8);
    self.textView.y = ScaleH(8);
    self.textView.height = self.height - ScaleH(16);
    self.textView.width = self.width - self.sendBtn.width - ScaleW(16);
}

#pragma mark - Lazy load

- (DDTextView *)textView
{
    if(!_textView)
    {
        _textView = [[DDTextView alloc] init];
        _textView.placehol = @"写下你的公告吧...";
        
        _textView.delegate = self;
//        _textView.backgroundColor = [UIColor colorWithHex:@"e5e5e5"];
        _textView.returnKeyType =  UIReturnKeyDefault;
//        _textView.layer.cornerRadius = 4;
        _textView.font = ScaleFont(13);
        _textView.textContainerInset = UIEdgeInsetsMake(8, 10, 0, 0);
    }
    return _textView;
}

- (UIView *)lineView
{
    if(!_lineView)
    {
        _lineView = [SGUtil newViewWithBgColor:[UIColor colorWithHex:@"e5e5e5"]];
        _lineView.height = 0.5;
    }
    return _lineView;
}
- (UIButton *)sendBtn
{
    if(!_sendBtn)
    {
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_sendBtn setBackgroundColor:[UIColor cm_mainColor]];
        _sendBtn.layer.cornerRadius = 4;
        _sendBtn.layer.masksToBounds = YES;
        _sendBtn.enabled = NO;
        _sendBtn.width = ScaleW(70);
        _sendBtn.height = ScaleH(34);
        _sendBtn.layer.cornerRadius = ScaleH(17);
        _sendBtn.layer.masksToBounds = YES;
        [_sendBtn addTarget:self action:@selector(sendBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _sendBtn;
}
@end
